package com.desksoft.ecommerce.util.sequence;

import java.util.HashMap;
import java.util.Map;


/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 2013-4-26
 * 创建作者：jh
 * 文件名称：SequenceUtils.java
 * 版本：
 * 功能：
 * 最后修改时间：
 * 修改记录：
 * </pre>
 *****************************************/

public class SequenceUtils {
	
	private static SequenceUtils _instance = new SequenceUtils();
	private Map<String, SequenceHelp> keyMap = new HashMap<String, SequenceHelp>(20); // Sequence载体容器
	private static final int POOL_SIZE = 10; // Sequence值缓存大小

	/**
	 * 禁止外部实例化
	 */
	private SequenceUtils() {
	}

	/**
	 * 获取SequenceUtils的单例对象
	 * 
	 * @return SequenceUtils的单例对象
	 */
	public static SequenceUtils getInstance() {
		return _instance;
	}

	/**
	 * 获取下一个Sequence键值
	 * 
	 * @param keyName
	 *            Sequence名称
	 * @return 下一个Sequence键值
	 */
	public synchronized long getNextKeyValue(String keyTableName) {
		SequenceHelp sequenceHelp = null;
		Long keyObject = null;
		try {
			if (keyMap.containsKey(keyTableName)) {
				sequenceHelp = keyMap.get(keyTableName);
			} else {
				sequenceHelp = new SequenceHelp(POOL_SIZE,keyTableName);
				keyMap.put(keyTableName, sequenceHelp);
			}
			keyObject = sequenceHelp.getNextKey();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return keyObject;
	}

	
	
	
}
